package com.inews.business.web;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.inews.business.entity.Buyer;
import com.inews.business.service.BuyerService;


@Controller
@RequestMapping(value = "/user")
public class BuyerRegController {
	
	private static final Logger logger = Logger.getLogger(BuyerRegController.class);

	@Autowired
	private BuyerService buyerService;
	
	
	@RequestMapping(value = "reg", method = RequestMethod.GET)
	public String userReg(Model model) { 
		return "user/userReg";
	}
	
	@RequestMapping(value = "saveBuyer", method = RequestMethod.POST)
	public String saveBuyer(Buyer buyer, Model model){
		String username = buyer.getUsername();
		if(buyerService.existBuyer(username)){
			model.addAttribute("error", "该用户已存在");
			return "user/userReg";
		}
		buyerService.saveBuyer(buyer);
		model.addAttribute("message", "注册用户成功");
		model.addAttribute("urladdress", "/");
		return "share/message";
	}
	
	
	@RequestMapping(value = "isUserExsit", method = RequestMethod.GET)
	public String isUserExsit(Buyer buyer, Model model){
		String username = buyer.getUsername();
		if(buyerService.existBuyer(username)){
			model.addAttribute("exsit", true);
		}
		return "user/checkuser";
	}
	
	
	
	
}
